package com.nus.fellowmark.modules.rbac.stp;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.convert.Convert;
import com.nus.fellowmark.entity.SysMenuEntity;
import com.nus.fellowmark.entity.SysRoleEntity;
import com.nus.fellowmark.modules.rbac.service.SysRoleMenuMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Custom permission verification interface extension
 *
 * @author Ruoyi Chen
 * @email chenruoyisz@gmail.com
 * @create 2023/2/11
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private SysRoleMenuMappingService sysRoleMenuMappingService;

    /**
     * Returns the set of permission codes owned by an account
     * @param userId
     * @param userType
     * @return
     */
    @Override
    public List<String> getPermissionList(Object userId, String userType) {
        List<SysMenuEntity> menus = sysRoleMenuMappingService.getMenuOrPermissionList(Convert.toInt(userId), userType);
        return menus.stream().map(SysMenuEntity::getCode).collect(Collectors.toList());
    }

    /**
     * Returns the set of role IDs owned by an account
     *
     * @param userId
     * @param userType
     * @return
     */
    @Override
    public List<String> getRoleList(Object userId, String userType) {
        List<SysRoleEntity> roles = sysRoleMenuMappingService.getRoleList(Convert.toInt(userId));
        return roles.stream().map(SysRoleEntity::getCode).collect(Collectors.toList());
    }
}
